package com.gonsin.gmessage.example.controllers;

import com.alibaba.fastjson.JSONObject;
import com.gonsin.gmessage.example.po.S10000NStatusDto;
import com.gonsin.gmessage.example.po.S30000UnitPo;
import com.gonsin.gmessage.example.po.TestMessagePo;
import com.gonsin.gmessage.example.sender.DemoSender;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Collections;

@RestController
@RequestMapping("/api/test")
public class TestController {

    // 注入消息发送器
    @Autowired
    private DemoSender demoSender;

    @GetMapping()
    public String send(HttpSession session) {
        // 直接调用方法
        TestMessagePo message = new TestMessagePo();
        message.setContent("test_content");
        demoSender.sendMicState("room_xxxxxxxxxxxx",
                Collections.singletonList("123"),
                Collections.singletonList(new S30000UnitPo()));
        return "ok";
    }

    @GetMapping("/batch")
    public String send2(HttpSession session) {

        new Thread() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    // 直接调用方法
                    TestMessagePo message = new TestMessagePo();
                    message.setContent("test_content");
                    demoSender.sendMicState("room_xxxxxxxxxxxx",
                            Collections.singletonList("123"),
                            Collections.singletonList(new S30000UnitPo()));
                }
            }
        }.start();

        // 直接调用方法
        TestMessagePo message = new TestMessagePo();
        message.setContent("test_content");
        demoSender.sendMicState("room_xxxxxxxxxxxx",
                Collections.singletonList("123"),
                Collections.singletonList(new S30000UnitPo()));
        return "ok";
    }

    @GetMapping("/num")
    public void testNumber() {
        String json = "{\"system_state\":{\"system_mode\":0,\"dis_mode\":0,\"dis_max\":0,\"tag_max\":0,\"lock\":0,\"server_language\":0,\"try_time\":0,\"voice_time\":0,\"reg_status\":{\"mode\":0,\"expect\":0,\"arrive\":0},\"vote_status\":{\"name\":\"\",\"type\":0,\"options_count\":0,\"max_select\":0,\"options\":[],\"vote_expect\":0,\"voted_num\":0,\"first_ot_last\":0}}}";
        S10000NStatusDto dto = JSONObject.parseObject(json, S10000NStatusDto.class);
        dto.setTest(1.23);
        demoSender.sendNumberTest(dto);
    }
}
